

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 类描述：
 *
 * @auther niezhuxe
 * @date 2020/3/26
 */
public class Demo {

    public static void main(String[] args) throws IOException {
        runImage();
        //要抓取图片的网址连接
//        String url = "https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%B6%AB%B7%BD%BD%F0%B9%DD%B3%A4&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111";
//        String url = "https://yavtube.com/photo/amazingly-lovely-babe-ashlyn-rae-undressing-and-teasing-her-pussy";
//        String url = "https://www.tuaoo.xyz/";
//        try {
//            SslUtils.ignoreSsl();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        //根据连接获取一个Connection对象
//        InputStream is = getConnection(url,null).getInputStream();
//        //调用commonsio工具包中IOUtils的方法，返回HTML内容；
//        String html = IOUtils.toString(is, "GBK");
//        String htmls = "";
        //解析HTML内容，获取所有图片链接地址
//        List<String> picPaths = parseHtml(html);
//        System.out.println("当前html内容："+html);
//        //判断是否获取到图片链接
//        if(picPaths.size() > 0) {
//            //创建一个线程池，处理下载任务
//            ExecutorService es = Executors.newFixedThreadPool(picPaths.size() < 50 ? picPaths.size() : 50);
//            //循环处理资源
//            for (final String picPath : picPaths) {
//                //根据具体的资源，创建下载任务，提交到线程池中
//                es.execute(()->{downLoad(pathHandle(picPath), "D:\\ImageSpace\\1",url);});
//            }
//            //关闭线程池
//            es.shutdown();
//        }
    }

    //解析html代码，下载代码中德图片
    public static void runImage(){

                try {
            SslUtils.ignoreSsl();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url = "https://m.yymh03.com/pages/#!/book/read/1224?chapter=235159";

        String html = "<section class=\"reader-cartoon-chapter\"><div class=\"reader-cartoon-image loaded\"><img src=\"https://kaj2hqu443.dayi58.com/b1360/c230806/30a991cf-169c-4b33-a63d-c664b2a4b806.jpg\" alt=\"圖-1\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-1 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://image2.dayi58.com/47ff2a85fd8a30a98f797318f0ff8cac/5f4288b4/b1360/c230806/468885e3-c363-41b3-90bf-3ba967dd0dc1.jpg\" alt=\"圖-2\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-2 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://kaj2hqu443.dayi58.com/b1360/c230806/55317356-84af-4e43-89af-6382831dfc51.jpg\" alt=\"圖-3\"><div class=\"preview\" style=\"padding-top: 461.667%;\"><span class=\"alt\">圖-3 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://image2.dayi58.com/f226d5c272c6f75ffe86070c024c60cf/5f4288b4/b1360/c230806/f0798e5f-7407-4b44-9deb-d4f7e27cd71b.jpg\" alt=\"圖-4\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-4 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://kaj2hqu443.dayi58.com/b1360/c230806/2a4b3a94-e3f5-4e2a-9fd4-1df569221dff.jpg\" alt=\"圖-5\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-5 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://image2.dayi58.com/1c97af97f84fc07a02ad2a929d30f778/5f4288b4/b1360/c230806/31f5075b-4d50-41ce-9f3d-b49cf6483fba.jpg\" alt=\"圖-6\"><div class=\"preview\" style=\"padding-top: 312.5%;\"><span class=\"alt\">圖-6 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://kaj2hqu443.dayi58.com/b1360/c230806/fe2d1b69-3486-400e-98f2-8f537521bd3c.jpg\" alt=\"圖-7\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-7 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://image2.dayi58.com/7578f40099b3d1e433452bb7ff3fb34a/5f4288b4/b1360/c230806/99ae5b1e-b911-41cd-8351-87aa811ced27.jpg\" alt=\"圖-8\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-8 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://kaj2hqu443.dayi58.com/b1360/c230806/c5852eb7-331f-4579-8d64-cf16245a57c7.jpg\" alt=\"圖-9\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-9 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://image2.dayi58.com/d2b0016d35cac60c95844d1438b6867e/5f4288b4/b1360/c230806/f41adb64-7bf3-42a7-a590-dc7af5cc6d40.jpg\" alt=\"圖-10\"><div class=\"preview\" style=\"padding-top: 333.333%;\"><span class=\"alt\">圖-10 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://kaj2hqu443.dayi58.com/b1360/c230806/d1653ab5-e914-4f40-b9a8-1dd724a41f7f.jpg\" alt=\"圖-11\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-11 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://image2.dayi58.com/63ac2fddfcb9063205f198f0d1398ab4/5f4288b4/b1360/c230806/2158e2c0-c37f-414f-9d32-b6ffdc70cf4d.jpg\" alt=\"圖-12\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-12 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://kaj2hqu443.dayi58.com/b1360/c230806/1ba848ec-5e8e-40e7-88ed-de4e89bbc9ff.jpg\" alt=\"圖-13\"><div class=\"preview\" style=\"padding-top: 583.333%;\"><span class=\"alt\">圖-13 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://image2.dayi58.com/cd319299f5e3be227fefff241ada8e79/5f4288b4/b1360/c230806/8686e264-4fcc-4e86-98ab-dae043c85d42.jpg\" alt=\"圖-14\"><div class=\"preview\" style=\"padding-top: 333.333%;\"><span class=\"alt\">圖-14 </span></div></div><div class=\"reader-cartoon-image loaded\"><img src=\"https://kaj2hqu443.dayi58.com/b1360/c230806/0c823efe-807a-46de-a681-56cbc92205ce.jpg\" alt=\"圖-15\"><div class=\"preview\" style=\"padding-top: 339.444%;\"><span class=\"alt\">圖-15 </span></div></div></section>";
















        //解析HTML内容，获取所有图片链接地址
        List<String> picPaths = parseHtml(html);
        System.out.println("当前html内容："+html);
        //判断是否获取到图片链接
        if(picPaths.size() > 0) {
//            //创建一个线程池，处理下载任务
//            ExecutorService es = Executors.newFixedThreadPool(picPaths.size() < 50 ? picPaths.size() : 50);
            //循环处理资源
            for (final String picPath : picPaths) {
                //根据具体的资源，创建下载任务，提交到线程池中
                downLoad(pathHandle(picPath), "D:\\ImageSpace\\14",url);
            }
        }
    }

    /**
     * 获取唯一序列号，做为文件名
     * @return
     */
    private static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-", "");
    }
    /**
     * 处理获取到的图片链接
     * @param picPath
     * @return
     */
    private static String pathHandle(String picPath) {
        if(!picPath.startsWith("http")) {
            picPath = "http:" + picPath;
        }
        //这个处理，是针对天猫的图片链接，用于下载大图;
        //天猫的商品图片链接示例如下：
        //http://img.alicdn.com/bao/uploaded/i4/TB19FGse7KWBuNjy1zjefkOypXa_032207.jpg_b.jpg
        //去掉最后一个_以后的内容，可以下载大图；否则就下载的是小图
        if(picPath.indexOf("_") != picPath.lastIndexOf("_")) {
            picPath = picPath.substring(0,picPath.lastIndexOf("_"));
        }
        return picPath;
    }

    /**
     * 下载图片
     * @param picPath
     * @param dir
     * @param referer
     */
    private static void downLoad(String picPath, String dir,String referer){
        try {
            //生成文件名
//            String name = getUUID()+"."+ FilenameUtils.getExtension(picPath);
            String name = new Date().getTime()+"."+ FilenameUtils.getExtension(picPath);
            FileUtils.copyToFile(getConnection(picPath,referer).getInputStream(), new File(new File(dir),name));
        } catch (IOException e) {
            System.err.println(picPath+ "下载失败！");
        }
    }

    /**
     * 使用正则表达式解析html内容，获取图片链接
     * @param html
     * @return
     */
    private static List<String> parseHtml(String html) {
        String regex = "\"[^\"^(^)^}^>^<^{]+\\.(jpg|png|jpeg|gif)";
        List<String> list = new ArrayList<>();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(html);
        while(m.find()) {
            list.add(m.group().substring(1));
        }
        return list;
    }

    /**
     * 根据url地址，获取一个连接对象，同时设置请求头，避免服务器防盗链，以及模拟浏览器请求
     * @param url
     * @param referer
     * @return
     */
    private static URLConnection getConnection(String url, String referer) {
        try {
            URLConnection uc = new URL(url).openConnection();
            //解决防盗链问题
            uc.setRequestProperty("referer", referer==null?"http://www.baidu.com/":referer);
            //模拟浏览器
            uc.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
            return uc;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(url);
            return null;
        }
    }
}
